package com.audi.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
import com.audi.BaseActivity;
import com.audi.adapter.MoveDeviceCardAdapter;
import com.audi.adapter.MoveToDeviceDialogAdapter;
import com.audi.adapter.SQLiteAsusNewHelper;
import com.audi.dto.DeviceItem;
import com.audi.dto.PlaceItem;
import com.audi.utils.Constants;
import com.audi.view.AudiDialog;
import com.audi.view.BaseDeviceDialog;


public class MoveDeviceInfoActivity extends BaseActivity {

	private ListView mListViewModeDeviceInfo;
	private MoveDeviceCardAdapter mMoveDeviceCardAdapter;
	ArrayList<DeviceItem>mListMoveDevice;
	Button mMoveto, cacelDialog, cancelActionBar;
	private ArrayList<PlaceItem>listPlaceItem;
	private CheckBox mCheckSelected;
	private ArrayList<DeviceItem>mListSelectDevice = new ArrayList<DeviceItem>();

	// letq Adapter
	private MoveToDeviceDialogAdapter list;
	SQLiteAsusNewHelper db;
	private MoveToDeviceDialogAdapter dialogAdapter;
	private ListView moveToDevice;
	TextView namePlace, createNewPlace;
	int placeId;
	String namePlaceCurrent;
	private AudiDialog dialogSelectCapability;
	private PlaceItem mAddPlaceItem;
	private SQLiteAsusNewHelper mSQLite;
	public static TextView mTitleMoveDevice;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_room_info_device);
		initActionBar(R.layout.action_bar_move_device);
		setDisableDrawer();
		initView();
		/*
		 * get data from database
		 */
		mListViewModeDeviceInfo.setAdapter(mMoveDeviceCardAdapter);
		moveToSetOnClick();
		setOnClickCanCel();

		/*
		 * move to device
		 */
	}

	public void initView() {
		mListViewModeDeviceInfo = (ListView) findViewById(R.id.listview_room_info);
		mListMoveDevice = (ArrayList<DeviceItem>) getIntent()
				.getSerializableExtra("dataDevice");
		mMoveDeviceCardAdapter = new MoveDeviceCardAdapter(this,
				mListMoveDevice);
		mCheckSelected = (CheckBox) findViewById(R.id.checkbox_device);
		namePlace = (TextView) findViewById(R.id.tv_name_room);
		db = new SQLiteAsusNewHelper(MoveDeviceInfoActivity.this);
		mMoveto = (Button) findViewById(R.id.btn_move_to);
		cancelActionBar = (Button) findViewById(R.id.btn_cancel);
		namePlace.setText((CharSequence) mListMoveDevice.get(0)
				.getDevicePlace());
		listPlaceItem = new ArrayList<PlaceItem>();
		mTitleMoveDevice = (TextView) findViewById(R.id.title_move_device);

	}

	public void moveToSetOnClick() {
		mMoveto.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(mMoveDeviceCardAdapter.getListDevice().size()>0) {
					listPlaceItem = db.getListPlace();
					final AudiDialog dialogMoveTo = new AudiDialog(
							MoveDeviceInfoActivity.this);
					dialogMoveTo.setLayout(R.layout.move_to_dialog);
					createNewPlace = (TextView) dialogMoveTo
							.findViewById(R.id.tv_move_to_create);
					moveToDevice = (ListView) dialogMoveTo
							.findViewById(R.id.lv_move_to_device);
					cacelDialog = (Button) dialogMoveTo
							.findViewById(R.id.btn_cancle_dialog_move_to);
					cacelDialog.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							dialogMoveTo.cancel();
						}
					});
					dialogAdapter = new MoveToDeviceDialogAdapter(
							MoveDeviceInfoActivity.this, listPlaceItem);
					moveToDevice.setAdapter(dialogAdapter);
					dialogMoveTo.show();
					int i = mMoveDeviceCardAdapter.getListDevice().get(0)
							.getDevicePlaceId();
					moveToDevice
							.setOnItemClickListener(new OnItemClickListener() {

								@Override
								public void onItemClick(AdapterView<?>arg0,
										View view, int position, long id) {
									// TODO Auto-generated method stub
									int oldId[] = new int[mMoveDeviceCardAdapter
											.getListDevice().size()];
									int newId = listPlaceItem.get(position)
											.getPlaceId();
									mListSelectDevice = mMoveDeviceCardAdapter
											.getListDevice();
									db.updatePlaceIdForListDevice(
											mListSelectDevice, newId);
									dialogMoveTo.cancel();

									/*
									 * get info current place
									 */
									namePlaceCurrent = mListMoveDevice.get(0)
											.getDevicePlace();
									placeId = mListMoveDevice.get(0)
											.getDevicePlaceId();
									Bundle bundleInfoPlace = new Bundle();
									bundleInfoPlace.putString(
											Constants.DATA_NAME_PLACE,
											namePlaceCurrent);
									bundleInfoPlace.putInt(
											Constants.DATA_ID_PLACE, placeId);
									Intent data = new Intent(
											MoveDeviceInfoActivity.this,
											PlaceCardDetailActivity.class);
									data.putExtras(bundleInfoPlace);
									startActivity(data);
									finish();
								}
							});

					/*
					 * set OnClick Create new place
					 */
					createNewPlace.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub

							new BaseDeviceDialog(
									MoveDeviceInfoActivity.this)
									.showSelectStatusPlace();

						}
					});

				} else
					Toast.makeText(MoveDeviceInfoActivity.this,
							"chua chon device", Toast.LENGTH_LONG).show();
			}

		});
	}

	public void setOnClickCanCel() {
		cancelActionBar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();

			}
		});
	}

	@Override
	public void onRefreshData() {
		// TODO Auto-generated method stub

	}

}
